package com.example.usercenter.once;

import cn.hutool.core.date.StopWatch;
import com.example.usercenter.model.domain.User;
import com.example.usercenter.service.UserService;
import io.netty.util.concurrent.CompleteFuture;
import io.netty.util.concurrent.FailedFuture;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;

/**
 * 批量插入用户
 * @author 米饭好好吃
 */
@Component
@Slf4j
public class BatchInsertUsers {

    private static final int TOTAL_COUNT = 100000;
    private static final int SINGLE_COUNT = 10000; // 每次插入10w

    @Resource
    private UserService userService;

    /**
     * 测试使用for循环插入10w条数据
     * 5s后执行一次
     */
//    @Scheduled(initialDelay = 5000, fixedRate = Long.MAX_VALUE)
    public void batchInsertUsersByFor() {
        // 开启定时器
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        for (int i = 0; i < 100000; i++) {
            User user = new User();
            user.setUserAccount("wumingshi");
            user.setUserPassword("2273ab7967825f4368d72a57c9179ad6");
            user.setUserTags("[\"Java\"]");
            user.setAvatarUrl("https://img1.baidu.com/it/u=1960525706,2419391089&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1714842000&t=261df0f623a83c76a92cfa6dbefc87af");
            user.setUserProfile("测试数据");
            user.setUsername("无名氏");
            user.setGender(0);
            user.setPhone("1784379482");
            user.setUserRole(0);
            user.setEmail("xxx@qq.com");
            userService.save(user);
        }
        stopWatch.stop();
        log.info("执行时间：" + stopWatch.getTotalTimeMillis());
    }

    /**
     * 测试多线程+批量插入10w条数据
     * 5s后执行一次
     */
//    @Scheduled(initialDelay = 5000, fixedRate = Long.MAX_VALUE)
    public void batchInsertUsersByBatchAndConcurrent() {
        // 开启定时器
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<CompletableFuture<Void>> futureList = new ArrayList<>();
        for (int i = 0; i < TOTAL_COUNT / SINGLE_COUNT; i++) {
            CompletableFuture<Void> future = CompletableFuture.runAsync(() -> {
                List<User> userList = new ArrayList<>();
                for (int j = 0; j < SINGLE_COUNT; j++) {
                    User user = new User();
                    user.setUserAccount("wumingshi");
                    user.setUserPassword("2273ab7967825f4368d72a57c9179ad6");
                    user.setUserTags("[\"Java\"]");
                    user.setAvatarUrl("https://img1.baidu.com/it/u=1960525706,2419391089&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1714842000&t=261df0f623a83c76a92cfa6dbefc87af");
                    user.setUserProfile("测试数据");
                    user.setUsername("无名氏");
                    user.setGender(0);
                    user.setPhone("1784379482");
                    user.setUserRole(0);
                    user.setEmail("xxx@qq.com");
                    userList.add(user);
                }
                userService.saveBatch(userList);
            });
            futureList.add(future);
        }
        CompletableFuture.allOf(futureList.toArray(new CompletableFuture[]{})).join();
        stopWatch.stop();
        log.info("执行时间：" + stopWatch.getTotalTimeMillis());
    }


    /**
     * 测试批量插入10w条数据
     * 5s后执行一次
     */
//    @Scheduled(initialDelay = 5000, fixedRate = Long.MAX_VALUE)
    public void batchInsertUsersByBatch() {
        // 开启定时器
        StopWatch stopWatch = new StopWatch();
        stopWatch.start();
        List<User> userList = new ArrayList<>();
        for (int i = 0; i < TOTAL_COUNT; i++) {
            User user = new User();
            user.setUserAccount("wumingshi");
            user.setUserPassword("2273ab7967825f4368d72a57c9179ad6");
            user.setUserTags("[\"Java\"]");
            user.setAvatarUrl("https://img1.baidu.com/it/u=1960525706,2419391089&fm=253&app=138&size=w931&n=0&f=JPEG&fmt=auto?sec=1714842000&t=261df0f623a83c76a92cfa6dbefc87af");
            user.setUserProfile("测试数据");
            user.setUsername("无名氏");
            user.setGender(0);
            user.setPhone("1784379482");
            user.setUserRole(0);
            user.setEmail("xxx@qq.com");
            userList.add(user);
        }
        userService.saveBatch(userList);
        stopWatch.stop();
        log.info("执行时间：" + stopWatch.getTotalTimeMillis());
    }
}
